package com.c2c.controller.user;

import com.c2c.po.Goods;
import com.c2c.po.Orders;
import com.c2c.po.User;
import com.c2c.service.GoodsService;
import com.c2c.service.OrderService;
import org.apache.ibatis.annotations.Many;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/Order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private GoodsService goodsService;

    //生成订单
    @RequestMapping("/addOrder")
    public ModelAndView userBuy(Orders orders, ModelAndView modelAndView, HttpSession httpSession) {
        //获取用户ID
        User userAccount = (User) httpSession.getAttribute("userAccount");
        orders.setUserAccount(userAccount.getUserAccount());
        //初始化时间
        orders.setOrderRiseTime(new Date());
        //随机数生成快递号
        int randomNumber = (int) (Math.random() * 1000000000) + 100000000;
        orders.setExpressId(randomNumber);
        //生成订单
        orderService.insertOrder(orders);
        modelAndView.setViewName("redirect:/Index/push");
        return modelAndView;
    }

    //跳转生成订单界面
    @RequestMapping("/orderDetail")
    public ModelAndView orderDetail(Orders orders, HttpSession httpSession, ModelAndView modelAndView) {
        //获取Goods信息
        int goodsId = orders.getGoodsId();
        Goods byId = goodsService.findById(goodsId);
        //获取用户全部信息
        User userAccount = (User) httpSession.getAttribute("userAccount");
        modelAndView.setViewName("pay");
        //传Goods信息
        modelAndView.addObject("byId", byId);
        //传User信息
        modelAndView.addObject("userAccount", userAccount);
        return modelAndView;
    }

    @RequestMapping("/updateOrderState")
    public ModelAndView updateOrderState(int state, Orders orderId, ModelAndView modelAndView){
        orderService.updateOrderStateByOrderId(orderId,state);
        modelAndView.setViewName("redirect:/userSell/myGoods");
        return modelAndView;
    }

}
